package com.mqz.seckill.rabbitmq;

import cn.hutool.json.JSONUtil;
import com.mqz.seckill.pojo.Order;
import com.mqz.seckill.pojo.SeckillMessage;
import com.mqz.seckill.pojo.User;
import com.mqz.seckill.service.GoodsService;
import com.mqz.seckill.service.OrderService;
import com.mqz.seckill.vo.GoodsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class RabbitConsumer {
    @Resource
    private GoodsService goodsService;
    @Resource
    private OrderService orderService;

    @RabbitListener(queues = "seckillQueue")
    public void consume(String message) {
//        log.info("接收到的消息: ", message);
        SeckillMessage seckillMessage = JSONUtil.toBean(message, SeckillMessage.class);
        User user = seckillMessage.getUser();
        Long goodsId = seckillMessage.getGoodsId();
        GoodsVO goodsVO = goodsService.getGoodsVOByGoodsId(goodsId);

        orderService.seckill(user, goodsVO);
    }
}
